//
//  SEGFangHaoListViewController.m
//  ExcellentHome
//
//  Created by 陈区 on 14-1-8.
//  Copyright (c) 2014年 陈区. All rights reserved.
//

#import "SEGFangHaoListViewController.h"
#import "SEGSubmitUserAtuhViewController.h"
#import "SEGHouseSelectCell.h"

@interface SEGFangHaoListViewController ()<UITableViewDelegate, UITableViewDataSource>
/// 表格
@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) SEGFangHaoListViewModel *viewModel;
@end

@implementation SEGFangHaoListViewController
#pragma mark - life cycle
- (instancetype)initWithViewModel:(SEGFangHaoListViewModel*)viewModel{
    if (self == [super init]) {
        _viewModel = viewModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showCustomNavigationBackButton];
    self.title = @"选择房间号";
    
    
    [self segInitViews];
    [self bindViewModel];
}

#pragma mark - Load Data
// 请求小区内所有房号列表

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.viewModel.myProfileManager.seg_seghousebyunitmodel count];
}
- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGHouseSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:kSEGHouseSelectCellIdentifier forIndexPath:indexPath];
    if (self.viewModel.myProfileManager.seg_seghousebyunitmodel.count > indexPath.row) {
        SEGHouseByUnitModel* model = [self.viewModel.myProfileManager.seg_seghousebyunitmodel objectAtIndex:indexPath.row];
        cell.houseInfoText = model.roomNo;
        if ([model.houseId isEqualToString:self.viewModel.selectedModel.houseId]) {
            cell.nameLabel.textColor = kColor_T;
        } else {
            cell.nameLabel.textColor = kColor_1;
        }
    }
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 显示选中状态
    self.viewModel.selectedModel = [self.viewModel.myProfileManager.seg_seghousebyunitmodel objectAtIndex:indexPath.row];
    [self.tableView reloadData];
    
    // 选完之后返回 SEGSubmitUserAtuhViewController
    NSArray *viewControllers = self.navigationController.viewControllers;
    for (NSUInteger i = viewControllers.count - 1; i > 0; i--) {
        UIViewController* viewController = viewControllers[i];
        
        BOOL canPop = NO;
        if ((self.viewModel.popClassName &&
        self.viewModel.popClassName.length >0 &&
        [viewController isKindOfClass:NSClassFromString(self.viewModel.popClassName)]) || [viewController isKindOfClass:[SEGSubmitUserAtuhViewController class]]) {
            canPop = YES;
        }
        if (canPop) {
            NSDictionary *dic = @{
                @"buildModel" : self.viewModel.buildModel,
                @"unitModel" : self.viewModel.unitModel,
                @"houseModel" : self.viewModel.selectedModel
            };
            [[NSNotificationCenter defaultCenter] postNotificationName:SEGSelectedHouseInfoNotification object:dic];
            
            [self.navigationController popToViewController:viewController animated:YES];
            break;
        }
    }
}


#pragma mark - Init Views
-(void)segInitViews{
    [self.view addSubview:self.tableView];
}

#pragma mark - Layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
}

#pragma mark - Private Methods

#pragma mark - RAC Data Binding
- (void)bindViewModel {
    //TODO: RAC拆包,NEED TEST!
    @weakify(self);
    [[[self.viewModel.requestDataCommand executionSignals] switchToLatest] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [SVProgressHUD dismiss];
        [self.tableView reloadData];
        NSLog(@"switchToLatest %@",x);
    }];
    
    [self.viewModel.requestDataCommand execute:nil];
    [SVProgressHUD show];
    
    [self.viewModel.messageSubject subscribeNext:^(NSString *  _Nullable x) {
        [SEGAlertView showTmepMessageWithMessage:x];
    }];
}

#pragma mark - Getter

- (UITableView *)tableView {
    if (!_tableView) {
        CGRect appbounds = CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight() - kNavigationBarAndStatusBarHeight);
        _tableView = [[UITableView alloc] initWithFrame:appbounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = kColor_L2;
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.backgroundColor = [UIColor clearColor];
        [_tableView registerClass:[SEGHouseSelectCell class] forCellReuseIdentifier:kSEGHouseSelectCellIdentifier];
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tableView;
}

#pragma mark setter
@end
